package com.natasa.progresspercent.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.Slider;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.bundle.AbilityInfo;
import ohos.global.configuration.Configuration;

import com.natasa.progresspercent.ResourceTable;
import com.natasa.progresspercent.lib.CircularProgress;
import com.natasa.progresspercent.lib.LineProgress;

/**
 * 内容页面
 *
 * @since 2021-07-15
 */
public class MainAbilitySlice extends AbilitySlice {
    private LineProgress lineProgress;
    private CircularProgress circularProgress;
    private Slider mSlider;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
mSlider = (Slider) findComponentById(ResourceTable.Id_seekbar);
ShapeElement shapeElement = new ShapeElement();
shapeElement.setRgbColor(new RgbColor(255, 64, 129));
shapeElement.setShape(ShapeElement.OVAL);
shapeElement.setBounds(0, 0, 40, 40);
mSlider.setThumbElement(shapeElement);
lineProgress = (LineProgress) findComponentById(ResourceTable.Id_line);
circularProgress = (CircularProgress) findComponentById(ResourceTable.Id_circular);
lineProgress.setRoundEdge(true);
lineProgress.setShadow(true);
lineProgress.setTypeface("Roboto-Regular.ttf");
        mSlider.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean ba) {
                lineProgress.setProgress(progress);
                circularProgress.setProgress(progress);
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });
        mSlider.setLayoutRefreshedListener(new Component.LayoutRefreshedListener() {
            @Override
            public void onRefreshed(Component component) {
                mSlider.setMaxValue(100);
            }
        });
    }

    private void onOrientationChanged() {
        Text text = (Text) findComponentById(ResourceTable.Id_title);
        if (getResourceManager().getConfiguration().direction != Configuration.DIRECTION_VERTICAL) {
            text.setHeight(dip2px(getContext(), 48));
            text.setTextSize(14, Text.TextSizeType.FP);
        } else {
            text.setHeight(dip2px(getContext(), 56));
            text.setTextSize(20, Text.TextSizeType.FP);
        }
    }

    @Override
    protected void onOrientationChanged(AbilityInfo.DisplayOrientation displayOrientation) {
        super.onOrientationChanged(displayOrientation);
        onOrientationChanged();
    }

    /**
     * vp转像素
     *
     * @param context 上下文
     * @param dipValue vp值
     * @return px 像素值
     */
    private int dip2px(Context context, float dipValue) {
        return DisplayManager.getInstance().getDefaultDisplay(context)
            .map(display -> (int) (display.getAttributes().densityPixels * dipValue + 0.5f))
            .orElse(0);
    }
}
